<?php
header ("Content-Type:text/html; charset= utf-8");
require_once ('config.php');
require_once (WEBURL.'/include/conn.php');
require_once (WEBURL.'/login_status_check.php');

if(empty($_POST['channel_id'])){
	echo "forbidden!";
	exit();
}
$channel_id=$_POST['channel_id'];
$channel_name=$_POST['channel_name'];
$pre_id=$_POST['pre_id'];

if(!checkLogin()){
	$gotopage=base64_encode("channel_remove.php?channel_id=$channel_id&pre_id=$pre_id&channel_name=$channel_name");
	header("Location:admin_login.php?gotopage=".$gotopage);
}
else{
	require_once (WEBURL.'/channel.class.php');
	$whether_channel_has_articles=$_POST['whether_channel_has_articles'];
	$sql_remove_channel="delete from channel where channel_id=$channel_id;";
	//echo $sql_remove_channel;
	$result_remove_channel=mysql_query($sql_remove_channel) or die(mysql_error());
	if($result_remove_channel){
		if($whether_channel_has_articles==1){
			if(empty($_POST['delete'])){
				/*转移文章*/
				$article_move_to_channel=$_POST['article_move_to_channel'];
				$sql_update_article_channel="update article_normal set article_main_channel=$article_move_to_channel where article_main_channel=$channel_id;";
				//echo $sql_update_article_channel;
				$result_update_article_channel=mysql_query($sql_update_article_channel) or die(mysql_error());
			 }else{
				/*删除文章*/
				$sql_delete_article="delete from article_normal where article_main_channel=$channel_id;";
				//echo $sql_delete_article;
				$result_delete_article=mysql_query($sql_delete_article) or die(mysql_error());
			}
		}
		if($pre_id!=0){
        /*若父栏目无其他子栏目，修改is_bottom*/
		$sql_pre_channel="select * from channel where pre_id=$pre_id;";
		$result_pre_channel=mysql_query($sql_pre_channel) or die(mysql_error());
		$num=mysql_num_rows($result_pre_channel);
		if($num==0){
			$sql_update_pre_channel="update channel set is_bottom=1 where channel_id=$pre_id;";
			$result_update_pre_channel=mysql_query($sql_update_pre_channel) or die(mysql_error());
		}
		}
		echo "<script language=JavaScript> alert('删除栏目成功！'); window.location=\"channel_show.php\"</script>";	;
	}
	}